﻿using ICCardHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControllerClient
{
    public partial class ReadCardForm : Form
    {
        public ReadCardForm()
        {
            InitializeComponent();
        }
        PCSCReader reader = null;
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                reader = new PCSCReader("ACS ACR1281 1S Dual Reader PICC 0");
                reader.startMonitor();
                //上电
                reader.upPower();

                string art = reader.getAtr();
                reader.upPower();
                string sw = reader.sendAPDU("802007000849434643434B4633");
                LogHelper.Debug("返回" + sw);
                sw = reader.sendAPDU("80200600083132333435363738");
                LogHelper.Debug("返回" + sw);
                sw = reader.sendAPDU("80200200084346434341433032");
                LogHelper.Debug("返回" + sw);
                sw = reader.sendAPDU("80A4000002AA01");
                LogHelper.Debug("返回" + sw);
                sw = reader.sendAPDU("80B200000A");
                LogHelper.Debug("返回" + sw);
                string cardnum = sw.Substring(0, sw.Length - 4);
                string cmp = sw.Substring(sw.Length - 4, 4);
                
                
                byte[] cardByte = HexUtils.strToHexByte(cardnum);
                cardnum = Encoding.Default.GetString(cardByte);
                LogHelper.Debug("卡号" + cardnum);
                //冷复位
                reader.coolReset();
                //下电
                reader.DownPower();
                if (cmp.Equals("9000"))
                {
                   
                    LogHelper.Debug("成功读取IC卡ID：[" + cardnum + "]");

                    System.Media.SystemSounds.Exclamation.Play();
                    string serverIp = "127.0.0.1";
                    try
                    {
                        FileWriter.writeTxt("8.txt", cardnum);
                        //serverIp = ConfigurationManager.AppSettings["ServerIP"].ToString();
                        FtpHelper ftpHelper = new FtpHelper(serverIp, "kcrx", "kcrx8888");
                        ftpHelper.ReadWriteTimeout = 1000;
                        ftpHelper.UsePassive = false;
                        if (ftpHelper.Upload(new FileInfo("8.txt"), "\\8.txt"))
                        {
                            MessageBox.Show("刷卡完成！卡号:" + cardnum);
                        }
                        else
                        {
                            LogHelper.Debug("业务机" + serverIp + "连接失败");
                            MessageBox.Show("业务机" + serverIp + "连接失败，请检查serverIp设置");
                        }

                        //FtpClient ftp = new FtpClient("kcrx", "kcrx8888");
                        //ftp.Connect(serverIp);
                        //ftp.UpLoadFile("8.txt","\\");
                    }
                    catch (Exception e2)
                    {
                        LogHelper.Debug("业务机" + serverIp + "连接失败" + e2.ToString());
                        MessageBox.Show("业务机" + serverIp + "连接失败，请检查serverIp设置");
                    }
                }
                else
                {
                    MessageBox.Show("读取IC卡ID失败：失败原因[" + sw + "]");
                }
            
            }
            catch (Exception e1)
            {
                MessageBox.Show("未搜索到卡片");
                LogHelper.Debug("el" + e1);
              
            }
          
         
         
        }

        private void ReadCardForm_Load(object sender, EventArgs e)
        {
           System.Media.SystemSounds.Exclamation.Play();
           this.ControlBox = false;
        }
    }
}
